#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "OLED.h"
#include "LED.h"
#include "ADSensor.h"
#include "Bell.h"
#include "Timer.h"
#include "Key.h"

//设1800-2300温度适宜
//设3500为黑夜

//时钟数据
int h=0;
int m=0;
int s=0;

uint8_t KeyNum=0;
int Mode=0;
int SetX=0;

void Xianshi();
void Clockjishi();
void Light_LED(int num);

int main(void)
{
	/*模块初始化*/
	OLED_Init();		//OLED初始化
	LED_Init();
	BELL_Init();
	Key_Init();
	AD_Init();
	
	
	Xianshi();
	for(int i=0;i<=3;i++){
		LED_ON(i);
	}
	bell(500);
	for(int i=0;i<=3;i++){
		LED_OFF(i);
	}
	
	Timer_Init();
	
	while (1)
	{
		
		if(Mode==0){
			TIM_Cmd(TIM3, ENABLE);	
			Xianshi();		
			OLED_ShowChar(2,15,' ');
			KeyNum=Key_GetNum();
			if(KeyNum==1){
				KeyNum=0;
				Mode=1;
			}
		}
		//setMODE
		else {
			OLED_ShowChar(2,15,'H');
			TIM_Cmd(TIM3,DISABLE);	
			while(Mode==1){
			KeyNum=Key_GetNum();
			if(KeyNum==1){
				Mode=0;
				SetX=0;
			}
			else if(SetX==0){
				//hour的设置
				OLED_ShowChar(2,15,'H');
					if(KeyNum==2){
						h++;
						if(h==24)h=0;
					}
					else if(KeyNum==3){
						h--;
						if(h==-1)h=23;
					}
					else if(KeyNum==4){
						SetX=1;
					}
				}
				else if(SetX==1){
					//min的设置
					OLED_ShowChar(2,15,'m');
					if(KeyNum==2){
						m++;
						if(m==60)m=0;
					}
					else if(KeyNum==3){
						m--;
						if(m==-1)m=59;
					}
					else if(KeyNum==4){
						SetX=2;
					}
				}
				else if(SetX==2){
					//s的设置
					OLED_ShowChar(2,15,'S');
					if(KeyNum==2){
						s++;
						if(s==60)s=0;
					}
					else if(KeyNum==3){
						s--;
						if(s==-1)s=59;
					}
					else if(KeyNum==4){
						SetX=0;
					}
				}
				Xianshi();
			}
		}
	}
}

void Xianshi(){
	//开头
	OLED_ShowString(1,1,"Holle,TQclock!");
	//时钟的显示
	OLED_ShowNum(2,1,h,2);
	OLED_ShowChar(2,3,':');
	OLED_ShowNum(2,4,m,2);
	OLED_ShowChar(2,6,':');
	OLED_ShowNum(2,7,s,2);
	if(Mode==1){
		OLED_ShowString(2,11,"set");
	}else{
		OLED_ShowString(2,11,"   ");
	}
	//AD的显示
	if(Mode==0){
		uint16_t Tem;
		OLED_ShowString(4, 1, "Tem:");
		Tem = AD_GetValue(ADC_Channel_0);
		if(Tem>1800&&Tem<2300){
			LED_ON(2);
			LED_OFF(1);
			LED_OFF(3);
		}if(Tem<1700){
			LED_ON(3);
			LED_OFF(2);
			LED_OFF(1);
		}if(Tem>2400){
			LED_ON(3);
			LED_OFF(1);
			LED_OFF(2);
		}
		OLED_ShowNum(4, 7, Tem, 4);
		
		uint16_t Lig;
		OLED_ShowString(3, 1, "Lig:");
		Lig = AD_GetValue(ADC_Channel_1);

		if(Lig>=3500){
			LED_ON(0);
		}
		if(Lig<3300){
			LED_OFF(0);
		}
		
		OLED_ShowNum(3, 7, Lig, 4);
		Delay_ms(10);
	}
	
}

/**
  * @brief  时钟计数函数，实现秒、分、时的进位功能
  * @param  hour: 指向小时变量的指针
  * @param  minute: 指向分钟变量的指针
  * @param  second: 指向秒钟变量的指针
  * @retval 无
  */
void Clockjishi()
{
    s++;
    if (s == 60)
    {
        s = 0;
        m++;
        if (m == 60)
        {
            m = 0;
            h++;
            if (h == 24)
            {
                h= 0;
            }
        }
    }
	 if(m==0&&s==0){GPIO_ResetBits(GPIOB, GPIO_Pin_15);}
	 else {GPIO_SetBits(GPIOB, GPIO_Pin_15);}
}

void tem_LED(int num){
	
}

void TIM3_IRQHandler(void)
{
	if (TIM_GetITStatus(TIM3, TIM_IT_Update) == SET)
	{
		// 在这里添加中断处理代码
		Clockjishi();
		// 清除TIM3中断标志位
		TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
	}
}
